//
//  MSView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2019/12/18.
//  Copyright © 2019 onemeeting. All rights reserved.
//

import Foundation

@objc protocol MSViewDelegate {
    @objc func taped(senber:MSView)
}

typealias MStateBlock = (MState)->()
typealias MTapBlock = (NSView)->()

enum MState {
    case normal
    case highlight
    case hover
    case selected
}


class MSView: NSView {
    
    var _tag:Int = 0
    override var tag: Int {
        get {
            return _tag
        }
        set(tag){
            _tag = tag
        }
    }
    
    var stateBlock:MStateBlock?
    var tap:MTapBlock?
    
    
    var isImpl = false
    
    weak var tapDelegate: MSViewDelegate?
    
    
    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        
        let optios:NSTrackingArea.Options = [
            NSTrackingArea.Options.mouseEnteredAndExited,
            NSTrackingArea.Options.mouseMoved,
            NSTrackingArea.Options.cursorUpdate,
            NSTrackingArea.Options.activeWhenFirstResponder,
            NSTrackingArea.Options.activeInKeyWindow,
            NSTrackingArea.Options.activeInActiveApp,
            NSTrackingArea.Options.activeAlways,
            NSTrackingArea.Options.assumeInside,
            NSTrackingArea.Options.inVisibleRect,
            NSTrackingArea.Options.enabledDuringMouseDrag
        ]
        
        let area = NSTrackingArea.init(rect: dirtyRect, options: optios, owner: self, userInfo: nil)
        self.addTrackingArea(area)
        self.becomeFirstResponder()
    }
    
    /*
    override func updateTrackingAreas() {
        let optios:NSTrackingArea.Options = [
                   NSTrackingArea.Options.mouseEnteredAndExited,
                   NSTrackingArea.Options.mouseMoved,
                   NSTrackingArea.Options.cursorUpdate,
                   NSTrackingArea.Options.activeWhenFirstResponder,
                   NSTrackingArea.Options.activeInKeyWindow,
                   NSTrackingArea.Options.activeInActiveApp,
                   NSTrackingArea.Options.activeAlways,
                   NSTrackingArea.Options.assumeInside,
                   NSTrackingArea.Options.inVisibleRect,
                   NSTrackingArea.Options.enabledDuringMouseDrag
               ]
               
        let area = NSTrackingArea.init(rect: self.bounds, options: optios, owner: self, userInfo: nil)
        self.addTrackingArea(area)
        self.becomeFirstResponder()
    }
    */
    

    
    override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        self.tapDelegate?.taped(senber: self)
        if stateBlock != nil {
            self.stateBlock!(.selected)
        }
        
        if tap != nil {
            self.tap!(self)
        }
        
        if isImpl == true {
            
            while true {
                guard let nextEvent = self.window?.nextEvent(matching: [.leftMouseUp, .leftMouseDragged]) else { continue }
                let mouseLocation = self.convert(nextEvent.locationInWindow, from: nil)
                let isInside = self.bounds.contains(mouseLocation)

                switch nextEvent.type {
                case .leftMouseDragged:
                     if isInside {
                         mouseDraggedImpl(with: nextEvent)
                     }
                case .leftMouseUp:
                     mouseUpImpl(with: nextEvent)
                     return

                default:
                    break
                }
            }
        }
        
    }
    
    
    func mouseDownImpl(with event: NSEvent) {
        
        //print("mouseDown")
    }
    
    func mouseDraggedImpl(with event: NSEvent) {
        
        //print("mouseDragged")
    }
    
    func mouseUpImpl(with event: NSEvent) {
        //print("mouseUp")
    }
    
    override func mouseUp(with event: NSEvent) {
        super.mouseEntered(with: event)
        if stateBlock != nil {
            self.stateBlock!(.highlight)
        }
    }
    
    override func mouseEntered(with event: NSEvent) {
        super.mouseEntered(with: event)
        if stateBlock != nil {
            self.stateBlock!(.hover)
        }
    }
    
    override func mouseExited(with event: NSEvent) {
        super.mouseExited(with: event)
        if stateBlock != nil {
            self.stateBlock!(.normal)
        }
    }
    
    /*
    override var acceptsFirstResponder: Bool {
        return true
    }
    */
}
